<template>
  <el-main>
    <div>
      <el-form ref="form" :model="form" label-width="100px">
        <el-form-item label="请输入航班号">
          <el-row>
            <el-col :span="50">
              <el-input style="width: 400px;height: 30px" v-model="form.dfltFlno"></el-input>
            </el-col>
          </el-row>
          <!-- <el-col :span="10">
            <el-button class="btn1" type="primary" @click="onSubmit"
              >查询</el-button
            >
          </el-col> -->
        </el-form-item>
        <!-- <div class="link-top"></div> -->
        <el-form-item label="起始站">
          <el-input style="width: 400px;height: 30px" v-model="form.apcd"></el-input>
        </el-form-item>

        <el-form-item label="日期">
          <el-col :span="13">
            <el-date-picker
              type="date"
              placeholder="选择日期"
              v-model="form.dtime"
              style="width: 200px;"
              @change="updateDateTime"
            ></el-date-picker>
          </el-col>
        </el-form-item>

        <el-form-item label="起飞/降落时间">
          <el-col :span="13">
            <el-time-picker
              placeholder="选择起飞时间"
              v-model="form.startDate"
              style="width: 200px;padding-right: 10px"
              :picker-options="{ start: '00:00', step: '00:15', end: '23:59' }"
    
            ></el-time-picker>
            <el-time-picker
              placeholder="选择降落时间"
              v-model="form.endDate"
              style="width: 200px"
              :picker-options="{ start: '00:00', step: '00:15', end: '23:59' }"
  
            ></el-time-picker>
          </el-col>
        </el-form-item>

        <el-form-item>
          <el-button type="primary" @click="onSubmit">搜索</el-button>
        </el-form-item>
      </el-form>
    </div>
    <!-- 数据表 -->
    <el-table
      ref="singleTable"
      :data="tableData"
      highlight-current-row
      style="width: 100%"
    >
      <el-table-column property="dfltFlno" label="航班号" width="100">
      </el-table-column>
      <el-table-column property="dfltAwcd" label="航空公司" width="200">
      </el-table-column>
      <el-table-column property="apcd" label="航线路由" width="150">
      </el-table-column>
      <el-table-column property="dtime" label="日期" width="200">
      </el-table-column>
      <el-table-column property="fptt" label="计划起飞时间" width="220">
      </el-table-column>
      <el-table-column property="fplt" label="预计降落时间" width="220">
      </el-table-column>
      <el-table-column label="操作" width="150" v-if="isManagenment">
        <template #default="scope">
          <el-button type="success" size="small" @click="handleEdit(scope.$index, scope.row)"
            >航班变更</el-button
          >
        </template>
      </el-table-column>
    </el-table>
    <!-- 分页组件 -->
    <el-pagination
      background
      layout="prev, pager, next"
      :total="count"
      :current-page="page"
      :pageCount="pageCount"
      @current-change="changePage"
    >
    </el-pagination>
  </el-main>

  <el-dialog v-model="dialogVisible" width="50%">
    <h2 style="text-align: center">{{ dialogTitle }}</h2>
    <br />
    <el-form
      :model="user"
      label-width="120px"
      :size="formSize"
      id="editForm"
      :rules="rules"
      ref="ediForm"
    >
      <el-form-item label="航班号" prop="dfltFlno">
        <el-input v-model="check.dfltFlno" />
      </el-form-item>
      <el-form-item label="航空公司" prop="dfltAwcd">
        <el-input v-model="check.dfltAwcd" />
      </el-form-item>
      <el-form-item label="航线路由" prop="apcd" >
        <el-input v-model="check.apcd" />
      </el-form-item>

      <el-form-item label="日期" prop="dtime" >
        <el-input v-model="check.dtime" />  
      </el-form-item>

      <el-form-item label="计划起飞时间" prop="fptt" >
        <el-input v-model="check.fptt" />
      </el-form-item>

      <el-form-item label="预计降落时间" prop="fplt" >
        <el-input v-model="check.fplt" />
      </el-form-item>

      <el-form-item label="原因" prop="flightStatus" >
        <el-input v-model="check.flightStatus" />
      </el-form-item>
    </el-form>
    <template #footer>
      <span class="dialog-footer">
        <el-button @click="cancel">取消</el-button>
        <el-button type="primary"  @click="confirm">
          确定修改
        </el-button>
      </span>
    </template>
  </el-dialog>
</template>
    
    <script>
    import { ElMessage, ElMessageBox } from 'element-plus'
    import { mapGetters } from "vuex";
export default {
  name: "Aairnonstop",
  computed: {
    ...mapGetters(['isAdmin']),
    ...mapGetters(['isManagenment']),
    ...mapGetters(['isAuditor']),
    ...mapGetters(['isAuditorAndMangement'])
  },
  data() {
    return {
      dialogVisible: false,
      dialogTitle: "修改航班",
      selectLabel: 0,
      tableDataType: 1,
      called:false,
      formLabelWidth: "120px", //表单文本宽度
      tableData: [{}], //图书分页数据
      count: 0, //记录总数
      page: 1, //当前页码
      pageCount: "", //总页数
      form: {
        dfltAwcd: "",
        dfltFlno: "",
        apcd: "",
        startDate: "",
        endDate: "",
        dtime: "2017-06-01T00:00:00.000Z",
      },
      check:{
        dfltFlno: "",
        dfltAwcd: "",
        apcd: "",
        dtime: "",
        fptt: "",
        fplt: "",
        
      },
    }
  },

  methods: {
    updateDateTime() {
    if (this.form.dtime) {
      const dtime = new Date(this.form.dtime);
      const selectedDate = new Date(dtime);
      const startDateTime = this.form.startDate ? new Date(this.form.startDate) : null;
      const endDateTime = this.form.endDate ? new Date(this.form.endDate) : null;

      selectedDate.setFullYear(dtime.getFullYear(), dtime.getMonth(), dtime.getDate());

      if (startDateTime) {
        selectedDate.setHours(startDateTime.getHours(), startDateTime.getMinutes(), startDateTime.getSeconds());
        this.form.startDate = selectedDate.toISOString();
      }

      if (endDateTime) {
        selectedDate.setHours(endDateTime.getHours(), endDateTime.getMinutes(), endDateTime.getSeconds());
        this.form.endDate = selectedDate.toISOString();
      }
    } else {
      this.form.startDate = '';
      this.form.endDate = '';
    }
  },
    changePage(currentPage) {
      this.page = currentPage;
      switch (this.tableDataType){
        case 1:
          this.fetchData();
          break;
        case 2:
          this.onSubmit();
          break;
      }
    },
    fetchData() {
      const pageNum = this.page;
      const pageSize = 10; // 设置每页显示的数据条数
      this.$axios
        .get("http://localhost:8888/dfdl/getAApno2", {
          params: { pageNum, pageSize },
        })
        .then((response) => {
          const result = response.data.data;
        
          this.count = result.size;
          this.pageCount = Math.ceil(result.total / pageSize);
          this.tableData = result.list;
          this.tableDataType = 1;
        })
        .catch((error) => {
          console.error(error);
        });
    },

    onSubmit() {
      const pageNum = this.page;
      const pageSize = 10;
      const dfltFlno = this.form.dfltFlno;
      const dtime = this.form.dtime;
      const apcd = this.form.apcd;
      const fptt = this.form.startDate;
      const fplt = this.form.endDate;
      const dfltAwcd = "";
      this.$axios
        .get("http://localhost:8888/dfdl/selectA2401", {
          params: {
            dfltFlno,
            dtime,
            dfltAwcd,
            apcd,
            fptt,
            fplt,
            pageNum,
            pageSize,
          },
        })
        .then((response) => {
          const result = response.data.data;
          console.info(result);
          if(response.data.status!=10000){
          
            this.$message.error({
              type:"error",
              message:"查询失败，没有数据"
            })
          }
        //   console.info(result);
          this.count = result.size;
          this.pageCount = Math.ceil(result.total / pageSize);
          this.tableData = result.list;
          if(result.list.length!=0 && !this.called){
            this.tableDataType=2;
            this.$message.success({
              type:"success",
              message:"查询成功"
            })
            this.called=true;
          }

        });
    },
    handleEdit(index, row) {
      this.dialogVisible=true;
      this.check=row;
    },
    
    cancel() {
      this.dialogVisible = false;
    },
    confirm(){
      ElMessageBox.confirm(
    '你确认要修改吗',
    'Warning',
    {
      confirmButtonText: '确定',
      cancelButtonText: '取消',
      type: 'warning',
    }
  )
    .then(() => {
      this.edit();
      //跳转页面
      setTimeout(() => {
      this.$router.push({ name: 'AairnonstopCheck' });
    }, 1000);

    })
    .catch(() => {
      ElMessage({
        type: 'info',
        message: '已经取消修改',
      })
      this.dialogVisible = false;
    })
    },
    edit(){
      const dfdlDtoMerge = this.check;
      console.info(dfdlDtoMerge);
      this.$axios
        .post("http://localhost:8888/dfdlcheck/checkA2", 
        dfdlDtoMerge,
        // 这块直接发送一个对象就行，里面包含所有信息和id，如果你外面又包裹了一层对象，那么后端接收的时候也要包裹一层对象，不然接收不到
        ).then((response) => {
          if(response.data.status==10000){
            this.$message.success({
              type:"success",
              message:"提交成功，正在前往审核页面"
            })
          }else{
            this.$message.error({
              type:"error",
              message:"修改失败"
            })
          }
        });
        }
  },

  mounted() {
    this.fetchData();
  },
};
</script>

<style scoped>
.btn1 {
  margin-left: 220px;
  margin-top: 15px;
}

.link-top {
  height: 10px;
  background-color: #ebeef5;
  margin-top: 20px;
}
</style>
